package edu.mse.camel.client.session.wizards;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;

public class TitleDescPage extends WizardPage implements Listener{

	IWorkbench workbench;
	IStructuredSelection selection;
	final IWorkspace workspace= ResourcesPlugin.getWorkspace();
	IStatus titleStatus;

	Text title;
	StyledText description;

	public TitleDescPage(IWorkbench workbench, IStructuredSelection selection) {
		super("StartPage");
		setTitle("Create a session");
		setDescription("Enter the title and description for the new session.");
		titleStatus = new Status(IStatus.OK, "not_used", 0, "", null);
		this.workbench = workbench;
		this.selection = selection;
		
	}

	public TitleDescPage(IWorkbench workbench) {
		super("StartPage");
		setTitle("Session Creation");
		setDescription("Enter the title and description for the new session.");
		this.workbench = workbench;
		
	}

	@Override
	public void createControl(Composite parent) {
	    // create the composite to hold the widgets
		GridData gd;
		Composite composite =  new Composite(parent, SWT.NULL);

	    // create the desired layout for this wizard page
		GridLayout gl = new GridLayout();
		int ncol = 2;
		gl.numColumns = ncol;
		composite.setLayout(gl);
		
		// Session Name				
		new Label (composite, SWT.NONE).setText("Session Name:");				
		title = new Text(composite, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol - 1;
		title.setLayoutData(gd);
		
		// Session Description
		new Label (composite, SWT.NONE).setText("Session Description:");				
		description = new StyledText(composite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		gd = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL);
		gd.horizontalSpan = ncol - 1;
		description.setLayoutData(gd);

	    // set the composite as the control for this page
		setControl(composite);		
		addListeners();

	}

	private void addListeners() {
		title.addListener(SWT.KeyUp, this);
		description.addListener(SWT.KeyUp, this);
	}

	@Override
	public void handleEvent(Event event) {

		titleStatus = workspace.validateName(title.getText().trim(), IResource.PROJECT);

		if(!titleStatus.isOK()){
			setErrorMessage(titleStatus.getMessage());
		} else {
			setErrorMessage(null);
		}

		getWizard().getContainer().updateButtons();
		
	}

	@Override
	public IWizardPage getNextPage() {

		CreateSessionWizard wizard = (CreateSessionWizard) getWizard();
		SessionModel model = wizard.model;
		model.title = getName(title);
		model.description = description.getText();
		ModelDiagramPage page = wizard.modelDiagramPage;
		return page;

	}

	@Override
	public IWizardPage getPreviousPage() {
		return null;
	}

	@Override
	public boolean canFlipToNextPage() {

		if (isTextNonEmpty(title) && titleStatus.isOK())
			return true;
		return false;
	}

	private static boolean isTextNonEmpty(Text t)
	{
		String s = t.getText();
		if ((s!=null) && (s.trim().length() >0)) return true;
		return false;
	}

	private String getName(Text textName) {
		String name = textName.getText().trim();
		if(name.length() == 0)
			return null;
		else
			return name;
	}

}
